//
//  ShopLayer.m
//  CarDemo
//
//  Created by huang kun on 8/12/10.
//  Copyright 2010 UESTC. All rights reserved.
//

#import "ShopLayer.h"
#define ktagCar 1

@implementation ShopLayer
@synthesize delegate;

- (void) dealloc {
	NSLog(@"deallocing %@",self);
	delegate = nil;
	[self removeAllChildrenWithCleanup:YES];
	[super dealloc];
}

- (id) init{
	if( (self = [super init]) ){
		CCSprite *bg = [CCSprite spriteWithFile:@"UI-shop.png"];
		[self addChild:bg];
		bg.position = ccp(240, 160);
		
		CCMenuItemImage *back = [CCMenuItemImage itemFromNormalImage:@"backToGame.png" selectedImage:@"backToGamePressed.png"
															  target:self selector:@selector(backToGame:)];
		
		CCMenuItemImage *item1 = [CCMenuItemImage itemFromNormalImage:@"shop1.png" selectedImage:@"shop1.png"
															  target:self selector:@selector(didSelect:)];
		
		CCMenuItemImage *item2 = [CCMenuItemImage itemFromNormalImage:@"shop2.png" selectedImage:@"shop2.png"
															  target:self selector:@selector(didSelect:)];
	
		CCMenuItemImage *item3 = [CCMenuItemImage itemFromNormalImage:@"shop3.png" selectedImage:@"shop3.png"
															  target:self selector:@selector(didSelect:)];
		
		CCMenuItemImage *item4 = [CCMenuItemImage itemFromNormalImage:@"shop4.png" selectedImage:@"shop4.png"
															  target:self selector:@selector(didSelect:)];
		
		CCMenuItemImage *item5 = [CCMenuItemImage itemFromNormalImage:@"shop5.png" selectedImage:@"shop5.png"
															  target:self selector:@selector(didSelect:)];
		
		CCMenu *menu = [CCMenu menuWithItems:back,item1,item2,item3,item4,item5,nil];
		[self addChild:menu];
		[menu setPosition:CGPointZero];
		[back setPosition:ccp(251, 61)];
		[item1 setPosition:ccp(162, 207)];
		[item2 setPosition:ccp(249, 207)];
		[item3 setPosition:ccp(337, 207)];
		[item4 setPosition:ccp(162, 127)];
		[item5 setPosition:ccp(249, 127)];
		item1.tag = 1;
		item2.tag = 2;
		item3.tag = 3;
		item4.tag = 4;
		item5.tag = 5;
}
	return self;
}

- (void) didSelect:(CCMenuItemImage *)item {
	if(item.tag >=1 && item.tag <= 5){
		index = item.tag;
		CCSprite *car = [CCSprite spriteWithFile:[NSString stringWithFormat:@"shop_%d.png",index]];
		[self addChild:car z:1 tag:ktagCar];
		[car setPosition:ccp(240, 160)];
		
		
		CCMenuItemImage *buy = [CCMenuItemImage itemFromNormalImage:@"buy001.png" selectedImage:@"buy002.png"
															   target:self selector:@selector(buy:)];
		
		CCMenuItemImage *back = [CCMenuItemImage itemFromNormalImage:@"back001.png" selectedImage:@"back002.png"
															 target:self selector:@selector(back:)];
		
		CCMenu *menu = [CCMenu menuWithItems:buy,back,nil];
		[car addChild:menu];
		[menu setPosition:CGPointZero];
		[buy setPosition:ccp(178, 45)];
		[back setPosition:ccp(303, 45)];
	}
}

- (void) buy:(id)sender {
	CCSprite *car = (CCSprite *)[self getChildByTag:ktagCar];
	[car removeAllChildrenWithCleanup:YES];
	[self removeChild:car cleanup:YES];
}

- (void) back:(id)sender {
	CCSprite *car = (CCSprite *)[self getChildByTag:ktagCar];
	[car removeAllChildrenWithCleanup:YES];
	[self removeChild:car cleanup:YES];
}

- (void) backToGame:(id)sender {
	[self removeAllChildrenWithCleanup:YES];
	[self.parent removeChild:self cleanup:YES];
}

@end
